package com.hzlx.service.impl;

import com.google.gson.Gson;
import com.hzlx.dao.TBusinessInfoDao;
import com.hzlx.dao.TFoodInfoDao;
import com.hzlx.dao.TUserInfoDao;
import com.hzlx.dao.impl.TBusinessInfoDaoImpl;
import com.hzlx.dao.impl.TFoodInfoDaoImpl;
import com.hzlx.dao.impl.TUserInfoDaoImpl;
import com.hzlx.entity.RespBean;
import com.hzlx.entity.TBusinessInfo;
import com.hzlx.entity.TFoodInfo;
import com.hzlx.entity.TUserInfo;
import com.hzlx.service.UserInfoService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @author 86166
 * @version 1.0
 * @description: TODO
 * @date 2023/4/12 16:08
 */
public class UserInfoServiceImpl implements UserInfoService {
    private static TUserInfoDao tUserInfoDao = new TUserInfoDaoImpl();
    private static TBusinessInfoDao tBusinessInfoDao = new TBusinessInfoDaoImpl();
    private static TFoodInfoDao tFoodInfoDao = new TFoodInfoDaoImpl();
    @Override
    public String login(HttpServletRequest request, HttpServletResponse response) {
        List<TUserInfo> tUserInfos = tUserInfoDao.selectUserByUserNameAndPassword(request.getParameter("userName"), request.getParameter("password"));
        TUserInfo tUserInfo = null;
        String url="";
        for (TUserInfo userInfo : tUserInfos) {
            tUserInfo = userInfo;
        }
        if (tUserInfos.size()>0){
            url = "userHome.do";
            request.getSession().setAttribute("tUserInfo",tUserInfo);
        }else {
            url="pages/user/userLogin.jsp";
        }
        return url;
    }

    @Override
    public String register(HttpServletRequest req) {
        try {
            req.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String nickName = req.getParameter("nickName");
        String userName = req.getParameter("userName");
        String password = req.getParameter("password");
        String tel = req.getParameter("tel");
        TUserInfo tUserInfo = new TUserInfo();
        tUserInfo.setNickName(nickName);
        tUserInfo.setUserName(userName);
        tUserInfo.setPassword(password);
        tUserInfo.setTel(tel);
        int rows = tUserInfoDao.insert(tUserInfo);
        String url="";
        if (rows>0){
            url="pages/user/login.jsp";
        }else {
            url="userRegister.do";
        }
        return url;
    }

    @Override
    public void goHome(HttpServletRequest req) {
        List<TBusinessInfo> tBusinessInfos = tBusinessInfoDao.selectBusinessAll();
        req.setAttribute("tBusinessInfos",tBusinessInfos);
    }

    @Override
    public void IntoBusiness(HttpServletRequest req) {
        try {
            req.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String userName = req.getParameter("userName");
        try {
            userName = new String(userName.getBytes("ISO-8859-1"),"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        List<TFoodInfo> tFoodInfos =  tFoodInfoDao.selectByUserNameFor(userName);
        req.setAttribute("tFoodInfos",tFoodInfos);
    }
}
